jsx3.Class.defineClass(
		"tibco.admin.monitormanage.uicomponents.text.nodeHeader.NodeHeader", //the full name of the class to create
		jsx3.gui.Block, //  extends
		null, // implements
		function (plugin)
		{
			plugin.prototype.isRunning;
			plugin.prototype.nodeId;
			plugin.prototype.nodeName;

			plugin.prototype.init = function()
			{
				this.jsxsuper();
			}

			plugin.prototype.getNamespace = function()
			{
				return "tibco.admin";
			}

			plugin.prototype.setUpComponent = function()
			{
				this.isRunning = false;
				var subject = tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure.nodeInfoCacheSubject;
				tibco.admin.monitormanage.general.PageBusUtils.subscribe(subject, this.handleDisplayScreen, this);
			}

			plugin.prototype.handleDisplayScreen = function(subject, message, headerObj)
			{
				headerObj.loadContent(headerObj);
			}

			plugin.prototype.loadContent = function(headerObj)
			{
				var nodeInfoDoc = tibco.admin.Main.getServer().Cache.getDocument(tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure.nodeScreenCacheId);
				if (nodeInfoDoc)
				{
					var record = nodeInfoDoc.getFirstChild();

					headerObj.nodeId = record.getAttribute("key");
					headerObj.nodeName = record.getAttribute("name");

					var nameField = headerObj.getDescendantOfName("nodeHeaderNodeTitle_MM_infra");
					nameField.setText(record.getAttribute("name"));
					nameField.repaint();

					var statusField1 = headerObj.getDescendantOfName("nodeHeaderNodeStatusLbl1_MM_infra");
					var statusField2 = headerObj.getDescendantOfName("nodeHeaderNodeStatusIcon_MM_infra");
					var statusField3 = headerObj.getDescendantOfName("nodeHeaderNodeStatusLbl2_MM_infra");
					var startStopButton = headerObj.getDescendantOfName("nodeHeaderStartStopBtn_MM_infra");
					var statusVal = record.getAttribute("status");
					var statusColor;
					var startStopButtonText = "";
					var imagePath;
					var server = tibco.admin.Main.getServer();
					var statusValidForButtons = false;

					if (statusVal == "Running")
					{
						statusColor = "#3A9848";
						imagePath = "jsxaddin://user!MatrixAdmin/images/matrix/started_running.gif";
						startStopButtonText = server.getDynamicProperty("@MM@general@headerStopBtnTitle");
						headerObj.getDescendantOfName("nodeHeaderStatusLayout").setCols(new Array('*',50,21,50,'*'),true);

						headerObj.isRunning = true;
						statusValidForButtons = true;
					}
					else// if(statusVal == "Stopped" || statusVal == "Installed/Stopped")
					{
						statusColor = "#FF0000";
						imagePath = "jsxaddin://user!MatrixAdmin/images/matrix/stopped.gif";
						startStopButtonText = server.getDynamicProperty("@MM@general@headerStartBtnTitle");
						headerObj.getDescendantOfName("nodeHeaderStatusLayout").setCols(new Array('*',50,21,100,'*'),true);
						
						headerObj.isRunning = false;
						statusValidForButtons = true;
					}

					var statusText1 = "<span style='color:#9898a5'>";
					statusText1 += server.getDynamicProperty("@MM@general@statusLabel");
					statusText1 += "</span>";
					statusField1.setText(statusText1);

					var statusText2;
					statusVal = tibco.admin.monitormanage.general.MonitorManageUtils.handleNodeStatusExternalization(statusVal);
					if (statusValidForButtons)
					{
						statusText2 = "&nbsp;&nbsp;<span style='color:" + statusColor + "'>" + statusVal + "</span>";
						statusField2.setSrc(imagePath);
						statusField2.setDisplay(jsx3.gui.Block.DISPLAYBLOCK);
						startStopButton.setDisplay(jsx3.gui.Block.DISPLAYBLOCK);
					}
					else
					{
						// the node is not in a state to be stopped or started yet
						statusText2 = "&nbsp;&nbsp;" + statusVal;
						statusField2.setDisplay(jsx3.gui.Block.DISPLAYNONE);
						startStopButton.setDisplay(jsx3.gui.Block.DISPLAYNONE);
					}

					statusField3.setText(statusText2);
					startStopButton.setText(startStopButtonText);
					headerObj.repaint();
				}
			}

			plugin.prototype.startStopNode = function()
			{
				try {
					var rulesPath;
					var dataPath;
					var operation;
					if (this.isRunning)
					{
						rulesPath = "tibco/admin/monitormanage/uicomponents/text/nodeHeader/rules/stopNodesRules.xml";
						dataPath = "tibco/admin/monitormanage/uicomponents/text/nodeHeader/data/stopNodesData.xml";
						operation = "stopNodes";
					}
					else
					{
						rulesPath = "tibco/admin/monitormanage/uicomponents/text/nodeHeader/rules/startNodesRules.xml";
						dataPath = "tibco/admin/monitormanage/uicomponents/text/nodeHeader/data/startNodesData.xml";
						operation = "startNodes";
					}

					if (tibco.admin.Main.getServer().getEnv("MODE")) {
						// mode is live
						objService = tibco.admin.Main.loadService(this.getNamespace(), rulesPath, operation);
						objService.setEndpointURL(tibco.admin.util.ServiceHelper.getNodeEndpointURL());
						// this setInboundURL should not be required...only here because of a current AMX framework bug
						objService.setInboundURL(dataPath);
					}
					else {
						objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), rulesPath), operation, null, null);
						objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), dataPath));
					}

					objService.nodeId = this.nodeId;
					objService.nodeName = this.nodeName;
					jsx3.log("Operation " + operation + " called with inputs " + this.nodeId + " and " + this.nodeName);

					//subscribe and call
					objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.onstartStopNodesSuccess);
					objService.subscribe(jsx3.net.Service.ON_ERROR, this.onstartStopNodesError);
					objService.subscribe(jsx3.net.Service.ON_INVALID, this.onstartStopNodesInvalid);
					objService.doCall();
				}
				catch(ex) {
					tibco.admin.Main.logException(ex, "Error in tibco.admin.monitormanage.uicomponents.text.nodeHeader.NodeHeader.startStopNode");
				}
			}

			plugin.prototype.onstartStopNodesSuccess = function(objEvent)
			{
				//var responseXML = objEvent.target.getInboundDocument();
				//jsx3.log("Success","The service call was successful.");

				// post a message to call getNode again so the status can be refreshed
				var message = { doRefresh:true };
				tibco.admin.monitormanage.general.PageBusUtils.publish(tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure.nodePerformRefreshSubject, message);
			};

			plugin.prototype.onstartStopNodesError = function(objEvent)
			{
				var myStatus = objEvent.target.getRequest().getStatus();
				jsx3.log("Error", "The service call failed. The HTTP Status code is: " + myStatus);
			};

			plugin.prototype.onstartStopNodesInvalid = function(objEvent)
			{
				jsx3.log("Invalid", "The following message node just failed validation:\n\n" + objEvent.message);
			};

		});